package com.teamwork.module01.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module01.Class;
import com.teamwork.common.entity.module01.Classroom;
import com.teamwork.common.result.Result;
import com.teamwork.module01.mapper.ClassroomMapper;
import com.teamwork.module01.service.ClassroomService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ClassroomServiceImpl extends ServiceImpl<ClassroomMapper, Classroom> implements ClassroomService {
    /**
     * 根据名称获取教室信息
     * @param num
     * @return
     */
    @Override
    public Result getByName(Integer num) {

        LambdaQueryWrapper<Classroom> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Classroom::getNum,num).or().like(Classroom::getNum,num);

        Classroom classroom = this.getOne(wrapper);
        if(Objects.isNull(classroom)){
            return Result.failure("该教室不存在");
        }

        return Result.success(classroom);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result getPage(Integer page, Integer size) {

        Page<Classroom> pageinfo = new Page<>(page,size);

        this.page(pageinfo);


        return Result.success(pageinfo);
    }

    /**
     * 添加或者修改
     * @param classroom
     * @return
     */
    @Override
    public Result InsertOrUpdate(Classroom classroom) {
        if(classroom.getId()!=null){
            //修改
            boolean b = this.updateById(classroom);
            if(!b) return Result.failure("修改失败");
        }
        //添加
        boolean b = this.save(classroom);
        if(!b) return Result.failure("添加失败");
        return Result.success("添加成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        return null;
    }

    /**
     * id数组批量删除
     * @param ids
     * @return
     */
    @Override
    public Result batchDelete(List<Integer> ids) {

        for (Integer id : ids) {
            boolean b = this.removeById(id);
           if(!b){
               return Result.failure("删除失败");
           }
        }
        return Result.success("删除成功");


    }
}

